package com.universalInterface.lims_interface.controller;


import cn.hutool.core.bean.BeanUtil;
import com.universalInterface.common.result.ApiResult;
import com.universalInterface.lims_interface.entity.InterfaceUser;
import com.universalInterface.lims_interface.service.IInterfaceUserService;
import com.universalInterface.lims_interface.vo.user.UserLoginVo;
import com.universalInterface.lims_interface.vo.user.UserSaveVo;
import com.universalInterface.lims_interface.vo.user.param.UserLoginParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 用户信息管理
 * </p>
 *
 * @author author
 * @since 2023-12-20
 */
@Tag(name = "用户信息")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class InterfaceUserController {

	private final IInterfaceUserService iInterfaceUserService;

	@Operation(summary = "新增用户")
	@PostMapping("/save")
	public ApiResult save(@Parameter(name = "UserSaveVo", description = "用户新增对象") @Valid @RequestBody UserSaveVo saveVo) {
		InterfaceUser user = iInterfaceUserService.save(BeanUtil.copyProperties(saveVo, InterfaceUser.class));
		return ApiResult.success("新增成功！");
	}

	@Operation(summary = "通过id删除用户")
	@DeleteMapping("/deleteById")
	public ApiResult deleteById(@Parameter(name = "id", description = "主键id") @NotNull @RequestParam(value = "id") Long id) {
		Integer i = iInterfaceUserService.deleteById(id);
		return ApiResult.success("删除成功！size = " + i);
	}

	@Operation(summary = "用户名密码登录校验")
	@PostMapping("/login")
	public ApiResult<UserLoginVo> login(@Parameter(description = "用户名密码登录对象") @Valid @RequestBody UserLoginParam param) {
		InterfaceUser data = iInterfaceUserService.login(BeanUtil.copyProperties(param, InterfaceUser.class));
		return ApiResult.success("登录成功！", BeanUtil.copyProperties(data, UserLoginVo.class));
	}
}
